import CryptoJS from 'crypto-js';

// 自定义工具插件
const install = (Vue) => {
	var aseKey = "tbyc1234" //秘钥必须为：8/16/32位
	Vue.prototype.$encode = function(message) {
		
		if(typeof message != "string") {
			message = String(message);
		}
		//加密 DES/AES切换只需要修改 CryptoJS.AES <=> CryptoJS.DES
		var encryptMsg = CryptoJS.AES.encrypt(message, CryptoJS.enc.Utf8.parse(aseKey), {
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		}).toString();
		return encryptMsg;
	}
	
	Vue.prototype.$decode = function(encryptMsg) {
		//解密
		var decryptMsg = CryptoJS.AES.decrypt(encryptMsg, CryptoJS.enc.Utf8.parse(aseKey), {
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		}).toString(CryptoJS.enc.Utf8);
		return decryptMsg;
	}
	
	Vue.prototype.$jumpPage = function(e) {
		let url;
		if(e.currentTarget) {
			url = e.currentTarget.dataset.url;
		} else {
			url = e;
		}
		uni.navigateTo({
			url
		})
	}
	
	Vue.filter('kg2ton', function(val) {
		return (Number(val) / 1000).toFixed(3);
	})
	Vue.filter('toFixed2', function(val) {
		return Number(val).toFixed(2);
	})
}

export default {
	install
}
